package Base;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
 
/*
 * @author Rosana
 *
 */
public class ImageTest {
 
	private static int IMG_WIDTH;
	private static int IMG_HEIGHT;
 
	public static void exec(String caminho, int width, int height){
 
            IMG_WIDTH = width;
            IMG_HEIGHT = height;
            
	try{
 
		BufferedImage originalImage = ImageIO.read(new File(caminho));
		int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
 
		BufferedImage resizeImageJpg = resizeImage(originalImage, type);
		ImageIO.write(resizeImageJpg, "png", new File(".\\src\\Imagens\\result.png")); 
 
	}catch(IOException e){
		System.out.println(e.getMessage());
	} 
    }
        
 
    private static BufferedImage resizeImage(BufferedImage originalImage, int type){
	BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
	Graphics2D g = resizedImage.createGraphics();
	g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
	g.dispose();        
	return resizedImage;
    }
    
    public static void writeByteArrayToFile(byte [] imagem){
        String strFilePath = ".\\src\\Imagens\\busca.png";
           
        try
        {
            FileOutputStream fos = new FileOutputStream(strFilePath);
           
            fos.write(imagem);

            fos.close();

        }
        catch(FileNotFoundException ex)
        {
            System.out.println("FileNotFoundException : " + ex);
        }
        catch(IOException ioe)
        {
            System.out.println("IOException : " + ioe);
        }
    }
 
}